//获取应用实例
var req = require('../../interfaceImpl.js');
var config = require('../../interfaceConfig.js');
var app = getApp();
var Record = [];
var giftList = [];
Page({
  data: {
    // 页面配置 

    winWidth: 0,
    winHeight: 0,
    // tab切换
    currentTab: 0,
  },
  onLoad: function () {

    var that = this;
    //  高度自适应
    that.setData({ points: app.globalData.loginuserinfo.points })
    wx.getSystemInfo({
      success: function (res) {
        var clientHeight = res.windowHeight,
          clientWidth = res.windowWidth,
          rpxR = 750 / clientWidth;
        var calc = clientHeight * rpxR - 90;
        console.log(calc)
        that.setData({
          winHeight: calc
        });
      }
    });
    return req.GetGiftList(app.globalData.token).then(res => {
      console.log(res)
      giftList = res.data.data;
      that.setData({ giftList: giftList })
    })
  },

  // 滑动切换tab
  swiperTab: function (e) {
    console.log(e.detail.current)
    if (e.detail.current == 1 && Record.length == 0) {
      //TODO请求兑换记录
      req.GetGiftRecord(app.globalData.token).then(res => {
        console.log(res)
        Record = res.data.data;
        this.setData({ Record: Record })
      })
    }
    var that = this;
    that.setData({ currentTab: e.detail.current });
  },
  // // 点击tab切换
  clickTab: function (e) {
    var that = this;
    if (this.data.currentTab === e.target.dataset.current) {
      return false;
    } else {
      that.setData({
        currentTab: e.target.dataset.current
      })
    }
  },
  //TODO兑换礼品
  getgift: function (e) {
    var that = this;
    // console.log(e.currentTarget.dataset.id);
    // if (app.globalData.loginuserinfo.points < e.currentTarget.dataset.point) {
    //   setTimeout(function () {
    //     wx.showToast({
    //       title: '积分不足',
    //       image: '/static/images/cuo.png',
    //       duration: 1500,
    //     })
    //   }, 10)
    //   return false;
    // }
    wx.showModal({
      title: '提示',
      content: '您确定要兑换该物品吗?兑换后无法取消！',
      success: function (res) {
        if (res.confirm) {
          wx.showLoading({
            title: '请稍候', mask: true
          })
          return req.GetGift(e.currentTarget.dataset.id, app.globalData.token).then(res => {
            console.log(res) //兑换礼品返回的结果
            wx.hideLoading()
            if (res.data.success) {
              return req.GetGiftList(app.globalData.token).then(res => {
                console.log(res)
                giftList = res.data.data;
                that.setData({ giftList: giftList })
              })
              wx.showToast({
                title: '兑换成功！',
                image: '/static/images/dui.png',
                duration: 1500,
                success: function () {
                  //TODO重新计算积分
                  that.setData({
                    currentTab: 1,
                    points: app.globalData.loginuserinfo.points - e.currentTarget.dataset.point
                  })
                }
              })
            } else {
              wx.showModal({
                title: '兑换失败',
                content: res.data.data,
                showCancel: false,
                success: function (res) {
                }
              })
            }
          })
        }
      }
    })
  }
})
